/*
 * LeftPane.java
 *
 * Created on 06.11.2008., 14.41
 */

package rs.fon.rapidminer.gui.panels.navigator;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;

import rs.fon.rapidminer.process.Step;

/**
 * 
 * @author Sasa
 */
public abstract class NavigatorPanel extends javax.swing.JPanel {

	private static final Color SELECTED_BUTTON_COLOR = new Color(20, 20, 20);
	private static final Color DEFAULT_BUTTON_COLOR = new Color(240, 240, 240);

	/** Buttons that represent steps **/
	private List<JButton> buttons = new LinkedList<JButton>();

	/** Button names used to identify action sources **/
	private String[] buttonNames;

	/** Steps that define this panel **/
	private List<Step> steps;

	/** Creates new form LeftPane */
	public NavigatorPanel() {
		initComponents();
	}

	protected abstract void performAction(Step step);

	public void load(List<Step> steps) {
		if (steps != null)
			clean();
		this.steps = steps;
		setButtons();
		drawButtons();
	}

	private void clean() {
		removeAll();
		buttons.removeAll(buttons);
		revalidate();
		repaint();
	}

	private void setButtons() {
		loadButtonNames();
		loadButtons();
	}

	private void loadButtonNames() {
		String[] names = new String[steps.size()];
		int i = 0;
		for (Step step : steps) {
			names[i++] = step.getName();
		}
		buttonNames = names;
	}

	private void loadButtons() {
		JButton b;
		int i = 0;
		for (String button : buttonNames) {
			b = new JButton(buttonNames[i]);
			b.setName(buttonNames[i++]);
			buttons.add(b);
		}
	}

	private void drawButtons() {
		addButtons();
		addActionEvents();
	}

	private void addButtons() {
		GridLayout layout = new GridLayout(buttons.size(), 0);
		setLayout(layout);
		layout.setVgap(0);

		for (JButton button : buttons) {
			add(button);
		}

		revalidate();
		repaint();
	}

	private void addActionEvents() {

		for (final JButton button : buttons) {
			button.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					performAction(getStepFromName(button.getName()));
					colorButton((JButton) e.getSource());
				}
			});
		}
	}

	private Step getStepFromName(String name) {
		for (Step step : steps) {
			if (step.getName().equals(name))
				return step;
		}
		return null;
	}

	private void colorButton(JButton selectedButton) {
		for (JButton b : buttons) {
			b.setBackground(DEFAULT_BUTTON_COLOR);
		}
		selectedButton.setBackground(SELECTED_BUTTON_COLOR);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		setBackground(new java.awt.Color(204, 204, 204));
		setName("Left pane\n"); // NOI18N

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 177,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 512,
				Short.MAX_VALUE));
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// End of variables declaration//GEN-END:variables

}
